package com.ruoyi.GroupControl.service.impl;
import com.ruoyi.GroupControl.domain.Group;
import com.ruoyi.GroupControl.mapper.GroupMapper;
import com.ruoyi.GroupControl.service.IGroupService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;

@Service
public class GroupServiceImpl implements IGroupService  {

    @Autowired
    private GroupMapper groupMapper;

    @Autowired
    private JdbcTemplate jdbcTemplate;   // spring-boot-starter-jdbc 自带

    /**
     * 查询最新日期的 wlddata 表
     */
    public List<Group> getLatestWldData() {
        // 1. 查询最新表名
        String latestTable = jdbcTemplate.queryForObject(
                "SELECT TOP 1 name FROM sys.tables WHERE name LIKE 'wlddata%' ORDER BY name DESC",
                String.class
        );
        if (StringUtils.isEmpty(latestTable)) {
            return Collections.emptyList();
        }

        // 2. 查询数据
        return groupMapper.selectLatestGroupData(latestTable);
    }

}